package cn.platform.response;

import lombok.Data;
import cn.platform.util.ErrorCode;

@Data
public class AResponse {
    private String message = "";
    private int code = ErrorCode.OK;
    private Object data = null;

    public AResponse() {
    }

    public AResponse(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 设置成功状态
     * @param success 是否成功
     */
    public void setSuccess(boolean success) {
        if (success) {
            this.code = ErrorCode.OK; // 成功状态码
            this.message = "操作成功";
        } else {
            this.code = ErrorCode.ERROR; // 默认错误状态码
            this.message = "操作失败";
        }
    }

    /**
     * 设置自定义状态码和消息
     * @param code 状态码
     * @param message 消息
     */
    public void setCodeAndMessage(int code, String message) {
        this.code = code;
        this.message = message;
    }

    //=============================== 新增方法不影响以前功能 ===========================

    public static AResponse success() {
        return new AResponse();
    }

    public static AResponse success(Object data) {
        AResponse res = new AResponse();
        res.setData(data);
        return res;
    }

    public static AResponse success(Object data, String message) {
        AResponse res = new AResponse();
        res.setData(data);
        res.setMessage(message);
        return res;
    }

    public static AResponse error(int code, String message) {
        AResponse res = new AResponse();
        res.setCode(code);
        res.setMessage(message);
        return res;
    }

    public static AResponse error(Object data, int code, String message) {
        AResponse res = new AResponse();
        res.setData(data);
        res.setCode(code);
        res.setMessage(message);
        return res;
    }

    public AResponse aSetData(Object data) {
        this.data = data;
        return this;
    }

    public AResponse aSetMessage(String message) {
        this.message = message;
        return this;
    }

    public AResponse aSetCode(int code) {
        this.code = code;
        return this;
    }

    //=============================== 新增方法不影响以前功能 ===========================
}

